﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cafe.DataAccess;

namespace Cafe.BusinessLogic
{
    class CafeManagementService
    {
        private User loginUser = null;
        public CafeManagementService(String username, String password)
        {
            Authorize(username, password);
        }
        public void Authorize(String username, String password)
        {
            DataClassesDataContext db=new DataClassesDataContext();
            var qry = from u in db.Users
                      where u.UserName == username && u.PassWord == Md5Parsing.GetMd5Hash(password)
                      select u;
            
            foreach (User u in qry)
            {
                loginUser = u;
                return;
            }
            throw new UserNotFoundException();
        }
        public IList<Cafe.DataObjectTransfer.Catalogue> GetCatalogues()
        {
            if (loginUser == null) throw new NotAuthorizedException();
            DataClassesDataContext db = new DataClassesDataContext();
            var catalogues = from c in db.Catalogues
                             select new Cafe.DataObjectTransfer.Catalogue
                             {
                                 CatalogueID=c.CatalogueID,
                                 CatalogueName=c.CatalogueName,
                                 Description=c.Description
                             };
            return catalogues.ToList<Cafe.DataObjectTransfer.Catalogue>();
        }
        public IList<Cafe.DataObjectTransfer.Desk> GetDesks()
        {

            DataClassesDataContext db = new DataClassesDataContext();

            var desks = from d in db.Desks
                        select new Cafe.DataObjectTransfer.Desk
                        {
                            DeskID = d.DeskID,
                            DeskName = d.DeskName,
                            Description = d.Description,
                        };
            IList<Cafe.DataObjectTransfer.Desk> list = desks.ToList<Cafe.DataObjectTransfer.Desk>();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].isDirty = db.DirtyDesks.Contains<DirtyDesk>(new DirtyDesk() { DeskID = list[i].DeskID });
            }
            return list;
        }
        
        public IList<Cafe.DataObjectTransfer.Product> GetProducts()
        {

            DataClassesDataContext db = new DataClassesDataContext();
            var products = from p in db.Products
                             select new Cafe.DataObjectTransfer.Product
                             {
                                 ProductID = p.ProductID,
                                 ProductName = p.ProductName,
                                 Description = p.Description,
                                 CatalogueID=p.CatalogueID,
                                 CatalogueName=p.Catalogue.CatalogueName,
                                 UnitId=p.UnitID,
                                 UnitName=p.Unit.UnitName,
                                 Price=p.Price,
                                 PromoPrice=p.PromoPrice,
                                 DateStart=p.DateStart,
                                 DateEnd=p.DateEnd
                             };
            return products.ToList<Cafe.DataObjectTransfer.Product>();
        }
        public IList<Cafe.DataObjectTransfer.Unit> GetUnits()
        {

            DataClassesDataContext db = new DataClassesDataContext();
            var units = from u in db.Units
                           select new Cafe.DataObjectTransfer.Unit
                           {
                               UnitID=u.UnitID,
                               UnitName=u.UnitName
                           };
            return units.ToList<Cafe.DataObjectTransfer.Unit>();
        }
        public void AddCatalogue(String name, String description)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            Catalogue catalogue = new Catalogue() { CatalogueName = name, Description = description };
            db.Catalogues.InsertOnSubmit(catalogue);
            db.SubmitChanges();
        }
        public void AddProduct(String name,String description,int catalogueID,int unitID,DateTime dateStart,DateTime dateEnd,decimal price, decimal promoPrice)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            Product product = new Product() { 
                ProductName = name, 
                Description = description, 
                CatalogueID = catalogueID, 
                UnitID = unitID, 
                DateStart = dateStart, 
                DateEnd = dateEnd, 
                Price = price, 
                PromoPrice = promoPrice 
            };
            db.Products.InsertOnSubmit(product);
            db.SubmitChanges();

        }
        public void AddDesk(String name, String description)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            Desk desk = new Desk() { DeskName = name, Description = description };
            db.Desks.InsertOnSubmit(desk);
            db.SubmitChanges();
        }
        public void UpdateCatalogue(int catalogueID, String name, String description)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            var catalogues = from c in db.Catalogues
                      where c.CatalogueID == catalogueID
                      select c;
            foreach (var catalogue in catalogues)
            {
                catalogue.CatalogueName = name;
                catalogue.Description = description;
            }
            db.SubmitChanges();

        }
        public void UpdateProduct(int productID,String name, String description, int catalogueID, int unitID, DateTime dateStart, DateTime dateEnd, decimal price, decimal promoPrice)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            var products = from p in db.Products
                             where p.ProductID == productID
                             select p;
            foreach (var product in products)
            {
                product.ProductName = name;
                product.Description = description;
                product.CatalogueID = catalogueID;
                product.UnitID = unitID;
                product.DateStart = dateStart;
                product.DateEnd = dateEnd;
                product.Price = price;
                product.PromoPrice = promoPrice;
            }
            db.SubmitChanges();
        }
        public void UpdateDesk(int deskID,String name,String description)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            var desks = from d in db.Desks
                             where d.DeskID == deskID
                             select d;
            foreach (var catalogue in desks)
            {
                catalogue.DeskName = name;
                catalogue.Description = description;
            }
            db.SubmitChanges();
        }
        public void DeleteCatalogue(int catalogueID)
        {
            
            DataClassesDataContext db = new DataClassesDataContext();
            var catalogues = from c in db.Catalogues
                             where c.CatalogueID == catalogueID
                             select c;
            foreach (var catalogue in catalogues)
            {
                db.Catalogues.DeleteOnSubmit(catalogue);
            }
            db.SubmitChanges();
        }
        public void DeleteProduct(int productID)
        {

            DataClassesDataContext db = new DataClassesDataContext();
            var products = from p in db.Products
                             where p.ProductID == productID
                             select p;
            foreach (var product in products)
            {
                db.Products.DeleteOnSubmit(product);
            }
            db.SubmitChanges();
        }
        public void DeleteDesk(int deskID)
        {

            DataClassesDataContext db = new DataClassesDataContext();
            var desks = from d in db.Desks
                             where d.DeskID == deskID
                             select d;
            foreach (var desk in desks)
            {
                db.Desks.DeleteOnSubmit(desk);
            }
            db.SubmitChanges();
        }
    }
}
